#! /usr/bin/python

################################################################################
#	\file   SConscript
#	\author Gregory Diamos <solusstultus@gmail.com>
#   \date   Saturday August 10, 2013
#	\brief  The SCons master build script for libcuxx
################################################################################

EnsureSConsVersion(1,2)

# Import Modules
from scripts.buildEnvironment   import CreateEnvironment
from scripts.buildNVEnvironment import CreateNVEnvironment

import os

# set environment variables
libcuxx_args = ARGUMENTS
libcuxx_cmd_line_targets = COMMAND_LINE_TARGETS
libcuxx_path_to_root_dir = ".."

# try to import an environment
env = CreateEnvironment(Variables())
env['path_to_root_dir'] = os.path.abspath(libcuxx_path_to_root_dir)

nvenv = CreateNVEnvironment(env)
 
# find all source files in the source tree
directories = ['gpu-native/util', 'gpu-native/runtime', 'gpu-native/driver',
	'gpu-native/entry']

source_directories = [os.path.join(i, 'implementation') for i in directories]
extensions = ['*.cpp']

sources = []
for dir in source_directories:
	for ext in extensions:
		regexp = os.path.join(dir, ext)
		sources.extend(env.Glob(regexp))

# create the library
libgpunative = env.SharedLibrary('gpunative', sources, LIBS=env['EXTRA_LIBS'])
gpunative_libs = ['gpunative'] + env['EXTRA_LIBS']

if env['install']:
	libgpunative = env.Install(os.path.join(env['install_path'], "lib"),
		libgpunative)

# create the tools
tools = []

tools.append(env.Program('gpu-native-loader', \
	['gpu-native/tools/gpu-native-loader.cpp'], LIBS=gpunative_libs))

for tool in tools:
	env.Depends(tool, libgpunative)

# install it all 
if env['install']:
	installed   = []
	executables = []

	print 'Installing GPU-Native and LibCXX'

	installed.append(env.Install(os.path.join(env['install_path'], "bin"),
		'scripts/which.py'))
	
	for tool in tools:
		executables.append(env.Install(
			os.path.join(env['install_path'], "bin"), tool))
	
	# install headers
	header_directories = [os.path.join(i, 'interface') for i in directories]
	header_extensions = ['*.h']
	
	headers = []
	for dir in header_directories:
		for ext in header_extensions:
			regexp = os.path.join(dir, ext)
			headers.extend(env.Glob(regexp))
	
	for header in headers:
		(directoryPath, headerName) = os.path.split( \
			os.path.relpath(str(header), env['path_to_root_dir']))
		installed.append(env.Install(os.path.join( \
			env['install_path'], "include", directoryPath), header))
	
	# set permissions
	for i in executables:
		env.AddPostAction(i, Chmod(i, 0755))
	for i in installed:
		env.AddPostAction(i, Chmod(i, 0644))
	
	# Run the install rules by default
	install_alias = env.Alias('install', env['install_path'])
	Default(install_alias)

if env['mode'] == 'debug':
	variant = '.debug_build'
else:
	variant = '.release_build'

# Dispatch to subprojects
SConscript(os.path.join(env['path_to_root_dir'], 'gpu-native', 'compiler', 'SConscript'), exports='env',
	variant_dir=os.path.join(env['path_to_root_dir'], variant, 'gpu-native', 'compiler'), duplicate=0)
SConscript(os.path.join(env['path_to_root_dir'], 'libc', 'SConscript'), exports='nvenv',
	variant_dir=os.path.join(env['path_to_root_dir'], variant, 'libc'), duplicate=0)
SConscript(os.path.join(env['path_to_root_dir'], 'libcxx', 'SConscript'), exports='nvenv',
	variant_dir=os.path.join(env['path_to_root_dir'], variant, 'libcxx'), duplicate=0)
SConscript(os.path.join(env['path_to_root_dir'], 'libcuxx', 'SConscript'), exports='nvenv',
	variant_dir=os.path.join(env['path_to_root_dir'], variant, 'libcuxx'), duplicate=0)


